package activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.List;

/**
 * @author Raih
 * @Date 2020/5/31
 */
public class Seventh2 {

    public static void main(String[] args) {
        ProcessEngine pe= ProcessEngines.getDefaultProcessEngine();

        RepositoryService res=pe.getRepositoryService();
        RuntimeService rus=pe.getRuntimeService();
        TaskService ts=pe.getTaskService();

        Deployment dep=res.createDeployment().addClasspathResource("VacationRequest.bpmn").deploy();

        //ProcessDefinition pd=res.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();

        ProcessInstance pi=rus.startProcessInstanceByMessage("My Message");

        Task queryTask=ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        System.out.println("Process Instance Information:");
        ProcessInstance queryPI=rus.createProcessInstanceQuery().singleResult();
        System.out.println("Process Instance ID:"+queryPI.getId()+", "
                + "Process Definition ID:"+queryPI.getProcessDefinitionId()+", "
                + "ActivityId ID:"+queryPI.getActivityId()+".");
        System.out.println("Execution Information:");
        List<Execution> exes=rus.createExecutionQuery().processInstanceId(pi.getId()).list();
        for(Execution exe:exes) {
            System.out.println("Execution ID:"+exe.getId()+", "
                    +  "Process Instance ID:"+exe.getProcessInstanceId()+", "
                    +  "Activity ID:"+exe.getActivityId());
        }
        System.out.println("Task["+queryTask.getName()+"] is finished.");
        ts.complete(queryTask.getId());

        System.out.println("Process Instance Information:");
        queryPI=rus.createProcessInstanceQuery().singleResult();
        System.out.println("Process Instance ID:"+queryPI.getId()+", "
                + "Process Definition ID:"+queryPI.getProcessDefinitionId()+", "
                + "ActivityId ID:"+queryPI.getActivityId()+".");
        System.out.println("Execution Information:");
        exes=rus.createExecutionQuery().processInstanceId(pi.getId()).list();
        for(Execution exe:exes) {
            System.out.println("Execution ID:"+exe.getId()+", "
                    +  "Process Instance ID:"+exe.getProcessInstanceId()+", "
                    +  "Activity ID:"+exe.getActivityId());
        }

        List<Task> queryTasks=ts.createTaskQuery().processInstanceId(pi.getId()).list();
        for(Task task:queryTasks){
            System.out.println("Task["+task.getName()+"] is finished.");
            ts.complete(task.getId());
            System.out.println("Process Instance Information:");
            queryPI=rus.createProcessInstanceQuery().singleResult();
            System.out.println("Process Instance ID:"+queryPI.getId()+", "
                    + "Process Definition ID:"+queryPI.getProcessDefinitionId()+", "
                    + "ActivityId ID:"+queryPI.getActivityId()+".");
            System.out.println("Execution Information:");
            exes=rus.createExecutionQuery().processInstanceId(pi.getId()).list();
            for(Execution exe:exes) {
                System.out.println("Execution ID:"+exe.getId()+", "
                        +  "Process Instance ID:"+exe.getProcessInstanceId()+", "
                        +  "Activity ID:"+exe.getActivityId());
            }
        }

        queryTask=ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        System.out.println("Process Instance Information:");
        queryPI=rus.createProcessInstanceQuery().singleResult();
        System.out.println("Process Instance ID:"+queryPI.getId()+", "
                + "Process Definition ID:"+queryPI.getProcessDefinitionId()+", "
                + "ActivityId ID:"+queryPI.getActivityId()+".");
        System.out.println("Execution Information:");
        exes=rus.createExecutionQuery().processInstanceId(pi.getId()).list();
        for(Execution exe:exes) {
            System.out.println("Execution ID:"+exe.getId()+", "
                    +  "Process Instance ID:"+exe.getProcessInstanceId()+", "
                    +  "Activity ID:"+exe.getActivityId());
        }
        ts.complete(queryTask.getId());
        System.out.println("Task["+queryTask.getName()+"] is finished.");

    }

}
